package com.poc.task;

import com.poc.service.TaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.task.listener.annotation.AfterTask;
import org.springframework.cloud.task.listener.annotation.BeforeTask;
import org.springframework.cloud.task.listener.annotation.FailedTask;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class TaskCommandLineRunner implements CommandLineRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(TaskCommandLineRunner.class);

    @Autowired
    private TaskService taskService;

    @BeforeTask
    public void beforeTask(TaskExecution taskExecution) {
        LOGGER.info("task start..{}", taskExecution.toString());
    }


    @AfterTask
    public void afterTask(TaskExecution taskExecution) {
        LOGGER.info("task end..{}", taskExecution.toString());
    }


    @FailedTask
    public void failedTask(TaskExecution taskExecution) {
        LOGGER.info("task fail..{}", taskExecution.toString());
    }


    public void run(String... args) throws Exception {
        LOGGER.info("input argument:{}", Arrays.toString(args));
        taskService.updateTask(Integer.valueOf(args[0]));
    }
}
